//
//  SettingSYNCViewController.m
//  clublbiza
//
//  Created by WANG TOM on 8/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SettingSYNCViewController.h"
#import "TMDataTable.h"
#import "ApiLogin.h"
#import "SLFoundation.h"

@interface SettingSYNCViewController ()

@end

@implementation SettingSYNCViewController
@synthesize txtFieldUrl;
@synthesize infoPanel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    AppMain *appMain = [AppMain shareInstance];
    self.txtFieldUrl.text = [appMain getGateWayURL:YES];
    
    self.infoPanel.text = @"";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.txtFieldUrl = nil;
    self.infoPanel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (IBAction)setGateWayUrl:(id)sender
{
    AppMain *appMain = [AppMain shareInstance];
    [appMain setGateWayURL:trim(self.txtFieldUrl.text)];
     self.infoPanel.text = @"更新网站IP地址成功，请点击 返回 按钮重新更新数据";
}
- (IBAction)resetGateWayUrl:(id)sender
{
    AppMain *appMain = [AppMain shareInstance];
    self.txtFieldUrl.text = [appMain getGateWayURL:YES];

}
- (IBAction)testGateWayUrl:(id)sender
{
    ApiLogin *apiLogin = [ApiLogin shareInstance];
    @try
    {
        [apiLogin testGateWay:trim(self.txtFieldUrl.text)];
        self.infoPanel.text = @"成功连接网站";
    }@catch (NSException *exception) {
        
        self.infoPanel.text = exception.reason;
        
        
    }
}
- (IBAction)deleteAllVideoButtonClick:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle: NSLocalizedString(@"确定删除",nil)
                          message: NSLocalizedString(@"确定删除已经下载的全部图片和视频吗",nil)
                          delegate: self
                          cancelButtonTitle: NSLocalizedString(@"确定",nil)
                          otherButtonTitles: NSLocalizedString(@"取消",nil), nil];
    [alert show];
    [alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    switch (buttonIndex) {
        case 0: 
        {       
            [self deleteAllVideo];
        }
            break;
            
        case 1: 
        {
            
        }
            break;
    }
}
- (void)deleteAllVideo
{
    TMDataTable *dt  = nil;
    NSString *defaultKey = nil;
    NSString *tableName = nil;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //Catalog
    tableName = @"Catalog";
    dt = [[TMDataTable alloc] initWithTablename:tableName withPKName:@"uuid" contextProvider:[ApiLogin shareInstance]];
    [dt deleteAll];
    [dt release];
    defaultKey = [NSString stringWithFormat:@"tbl_current_version_%@",tableName];
    [userDefaults removeObjectForKey:defaultKey];
    
    
    //Sub Catalog
    tableName = @"Catalog_sub";
    dt = [[TMDataTable alloc] initWithTablename:tableName withPKName:@"uuid" contextProvider:[ApiLogin shareInstance]];
    [dt deleteAll];
    [dt release];
    defaultKey = [NSString stringWithFormat:@"tbl_current_version_%@",tableName];
    [userDefaults removeObjectForKey:defaultKey];
    
    //Video
    tableName = @"Catalog_sub_video";
    dt = [[TMDataTable alloc] initWithTablename:tableName withPKName:@"uuid" contextProvider:[ApiLogin shareInstance]];
    [dt deleteAll];
    [dt release];
    defaultKey = [NSString stringWithFormat:@"tbl_current_version_%@",tableName];
    [userDefaults removeObjectForKey:defaultKey];
    
    
    //Resource
    tableName = @"Resource";
    dt = [[TMDataTable alloc] initWithTablename:tableName withPKName:@"uuid" contextProvider:[ApiLogin shareInstance]];
    [dt deleteAll];
    [dt release];
    defaultKey = [NSString stringWithFormat:@"tbl_current_version_%@",tableName];
    [userDefaults removeObjectForKey:defaultKey];
    
    
    //delete files
    NSString *fileDirPath = [AppMain resourceFolderPath];
    NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:fileDirPath];
    NSString *fileName;
    NSError *error;
    while (fileName= [dirEnum nextObject]) {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@",fileDirPath,fileName] error:&error];
    } 
}
@end
